<template>
  <!-- 循环遍历 -->
  <el-submenu
    v-if="menu.list && menu.list.length >= 1"
    :index="menu.menuId + ''"
    :popper-class="'site-sidebar--' + sidebarLayoutSkin + '-popper'"
  >
    <template slot="title">
      <!-- <icon-svg :name="menu.icon || ''"
                class="site-sidebar__menu-icon"></icon-svg> -->
      <img
        :src="
          menu.is_default == 0
            ? `../../static/img/${menu.url}2@2x.png`
            : `../../static/img/${menu.url}@2x.png`
        "
        class="icon_menu"
        alt=""
      />
      <span>{{ menu.name }}</span>
    </template>
    <sub-menu
      v-for="item in menu.list"
      :key="item.menuId"
      :menu="item"
      :dynamicMenuRoutes="dynamicMenuRoutes"
    >
    </sub-menu>
  </el-submenu>
  <!-- 这就是导航栏 -->
  <el-menu-item
    v-else
    :default-active="$route.path"
    :router="true"
    :index="menu.menuId + ''"
    @click="gotoRouteHandle(menu)"
    :class="menu.is_default == 0 ? 'is-actives' : 'is-active'"
  >
    <!-- 这是图标 -->
    <!-- <icon-svg :name="menu.icon || ''"
              class="site-sidebar__menu-icon"></icon-svg> -->
    <img
      :src="
        menu.is_default == 0
          ? `../../static/img/${menu.url}2@2x.png`
          : `../../static/img/${menu.url}@2x.png`
      "
      class="icon_menu"
      alt=""
    />
    <span>{{ menu.name }}</span>
  </el-menu-item>
</template>

<script>
import SubMenu from './main-sidebar-sub-menu'
export default {
  name: 'sub-menu',
  data() {
    return {
      route: ''
    }
  },
  props: {
    menu: {
      type: Object,
      required: true
    },
    dynamicMenuRoutes: {
      type: Array,
      required: true
    }
  },
  components: {
    SubMenu
  },
  computed: {
    sidebarLayoutSkin: {
      get() { return this.$store.state.common.sidebarLayoutSkin }
    }
  },
  methods: {
    // 通过menuId与动态(菜单)路由进行匹配跳转至指定路由
    gotoRouteHandle(menu) {
      // console.log('menu', menu)
      var route = this.dynamicMenuRoutes.filter(item => item.meta.menuId === menu.menuId);
      var id = menu.id;
      var menuList = JSON.parse(sessionStorage.getItem('menuList') || '[]');
      for (var i in menuList) {
        id == menuList[i].id ? menuList[i].is_default = 1 : menuList[i].is_default = 0
      }
      this.$store.commit('common/updateMenuList', menuList);
      var arr = JSON.stringify(menuList)
      sessionStorage.setItem('menuList', arr)
      if (route.length >= 1) {
        this.$router.push({ name: route[0].name })
      }
    }
  }
}
</script>
<style lang="scss" scoped>
.icon_menu {
  width: 20px;
  height: 20px;
  margin: 0 7px;
}
</style>
